package com.neymar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neymar.entity.Company;
import com.neymar.entity.Position;
import com.neymar.entity.User;
import com.neymar.mapper.CompanyMapper;
import com.neymar.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neymar.util.PageUtils;
import com.neymar.vo.CompanyVO;
import com.neymar.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公司表 服务实现类
 * </p>
 *
 * @author neymar
 * @since 2021-06-02
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public int save(Company company) {
        return companyMapper.insert(company);
    }

    @Override
    public int deleteCompanyById(String companyId) {
        return companyMapper.deleteById(companyId);
    }

    @Override
    public int updateCompanyById(Company company) {
        return companyMapper.updateById(company);
    }

    @Override
    public Company findCompanyById(String companyId) {
        return companyMapper.selectById(companyId);
    }

    @Override
    public CompanyVO findAllCompany(Integer current, Integer size, String name) {

        // 分页查询
        IPage<Company> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);
        QueryWrapper<Company> wrapper = new QueryWrapper<>();
        wrapper.like("company_name", name);
        iPage = companyMapper.selectPage(iPage, wrapper);

        CompanyVO companyVO = new CompanyVO();
        companyVO.setCurrent(current);
        companyVO.setSize(size);
        companyVO.setPages(iPage.getPages());
        companyVO.setTotal(iPage.getTotal());
        companyVO.setCompany(iPage.getRecords());
        return companyVO;
    }

    @Override
    public CompanyVO findCompanyDetails(String companyId, Integer current, Integer size) {

        // 获取记录
        List<Map<String, String>> details = companyMapper.findCompanyDetails(companyId, current, size);
        CompanyVO companyVO = new CompanyVO();
        companyVO.setCurrent(current);
        companyVO.setSize(size);
        companyVO.setCompanyDetails(details);

        Long total = (long) details.size();

        // 获取总的页数
        Long page = PageUtils.getPage(total, size);
        companyVO.setPages(page);
        companyVO.setTotal(total);

        return companyVO;
    }
}
